package com.xiaoshangkou.hellopra.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.pojo.Permission;
import com.xiaoshangkou.hellopra.pojo.Permission2;
import com.xiaoshangkou.hellopra.service.Permission2Service;
import com.xiaoshangkou.hellopra.service.PermissionServeice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/backstage/permission2/")
public class Permission2Controller {
    @Autowired
    private Permission2Service permission2Service;
    /**
     * 用途：进入权限页面时分页查询所有权限
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1")int page,
                            @RequestParam(defaultValue = "6")int size){
        ModelAndView modelAndView=new ModelAndView();
        Page<Permission2> permissionPage = permission2Service.findPermission2(page, size);
        modelAndView.addObject("permissionPage",permissionPage);
        modelAndView.setViewName("/backstage/permission2_all");
        return modelAndView;
    }

    /**
     * 用途：添加新的权限
     * permission2 :前端传来的权限对象
     * 待更新：后期前端页面通过json数据实现局部更新
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add(Permission2 permission2){
        permission2Service.add(permission2);
        return "添加成功";
    }

    /**
     * 用途：删除角色
     * @param peid2 前端传来的要删除角色的peid
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer peid2){
        permission2Service.delete(peid2);
        return "redirect:/backstage/permission2/all";
    }

    /**
     *用途：在修改管理员之前，先将原来的数据返回到前端
     *peid2:权限的peid2
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Permission2 edit(Integer peid2){
        Permission2 permission2 = permission2Service.findById(peid2);
        return permission2;
    }

    @RequestMapping("/update")
    public String update(Permission2 permission2){
        permission2Service.update(permission2);
        return "redirect:/backstage/permission2/all";
    }
}
